#ifndef JGLOGTOOLS_H_
#define JGLOGTOOLS_H_

#include <stdarg.h>
#include "stdio.h"
#include <time.h>
#include <string.h>

#ifdef _WIN32
#include <windows.h>
#else
#include <pthread.h>
#endif

#ifdef __cplusplus
extern "C"
{
#endif

#define JGLOGTOOLS_VBBUF_LEN 1024
#define JGLOGTOOLS_TIMEBBUF_LEN 20
#define JGLOGTOOLS_LOGBBUF_LEN 1224
#define JGLOGTOOLS_FILENAME_LEN 64

    typedef enum _JGLogTools_LEVEL
    {
        JGLOGTOOLS_LEVEL_DEBUG = 0,
        JGLOGTOOLS_LEVEL_RINFO,
        JGLOGTOOLS_LEVEL_RWARNING,
        JGLOGTOOLS_LEVEL_RERROR
    } JGLogTools_LEVEL;

    typedef enum _JGLogTools_OUTSTATE
    {
        JGLOGTOOLS_ONLY_CONSOLE = 0,
        JGLOGTOOLS_ONLY_FILE,
        JGLOGTOOLS_BOTH,
        JGLOGTOOLS_BOTHN_EVERR
    } JGLogTools_OUTSTATE;

    typedef struct _JGLogTools
    {
        // 0:console,1:file,2:both,3:both never
        JGLogTools_OUTSTATE _outState;
        JGLogTools_LEVEL _level;
        char _vbuf[JGLOGTOOLS_VBBUF_LEN];
        char _logbuf[JGLOGTOOLS_LOGBBUF_LEN];
        char _timeBuf[JGLOGTOOLS_TIMEBBUF_LEN];
        char _filename[JGLOGTOOLS_FILENAME_LEN];
        char _filepath[JGLOGTOOLS_FILENAME_LEN];
        struct tm _pfileTime;
        struct tm *_nowPfileTime;
        FILE *_fp;

#ifdef _WIN32
        HANDLE g_hMutex;
#else
        pthread_mutex_t g_hMutex;
#endif

    } JGLogTools;

    static JGLogTools JGLogToolsBase;

    void JGLogTools_Init(JGLogTools_LEVEL level, JGLogTools_OUTSTATE outState);
    void JGLogTools_Clear();

    void JGLogTools_CreateLogFile();

    void JGLogTools_Log(JGLogTools_LEVEL level, const char *fileDesc, int lineNum, const char *data, ...);
    char *_JGLogTools_getLevelRemark(JGLogTools_LEVEL *level);
    char *_JGLogTools_GetNowTimeStr(char *strformat);
    void _JGLogTools_WriteFile(char *strlog);
    void _JGLogTools_Lock();
    void _JGLogTools_UnLock();


#define JGLOG_DEGBUG(...) JGLogTools_Log(JGLOGTOOLS_LEVEL_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
#define JGLOG_RINFO(...) JGLogTools_Log(JGLOGTOOLS_LEVEL_RINFO, __FILE__, __LINE__, __VA_ARGS__)
#define JGLOG_RWARNING(...) JGLogTools_Log(JGLOGTOOLS_LEVEL_RWARNING, __FILE__, __LINE__, __VA_ARGS__)
#define JGLOG_RERROR(...) JGLogTools_Log(JGLOGTOOLS_LEVEL_RERROR, __FILE__, __LINE__, __VA_ARGS__)

#ifdef __cplusplus
}
#endif

#endif // !JGLOGTOOLS_H_
